package com.cyam.controller;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.cyam.domain.Article;
import com.cyam.domain.ArticleFile;
import com.cyam.domain.ArticleType;
import com.cyam.service.ArticleFileService;
import com.cyam.service.ArticleService;
import com.cyam.service.ArticleTypeService;
import com.cyam.util.Pager;
import com.cyam.util.UploadResult;
import com.cyam.util.UploadUtil;

/**
 * 
 * @author CourageAQ
 * 文件上传功能
 */
@Controller
@RequestMapping("admin")
public class UploadController {
	
	@Resource
	private ArticleService articleService;
	
	@Resource
	private ArticleTypeService articleTypeService;
	
	@Resource
	private ArticleFileService articleFileService;
	
	/**
	 * 上传之后跳转到列表页面
	 * @param file
	 * @param request
	 * @param ac
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/upload")
	public String uploadFile(@RequestParam(value = "file", required = false) MultipartFile file,
			HttpServletRequest request,Article ac,Model model) throws Exception{
		String view = "";
		HttpSession session = request.getSession();
		if(session.getAttribute("username") != null) {
			String resultJson = UploadUtil.uploadFile(file, request,null);
			UploadResult uploadResult = JSON.parseObject(resultJson, UploadResult.class);
			try {
				if (!uploadResult.getCode().equals("500")) {
					ac.setArticle_time(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
					ac.setClicknum(0);
					articleService.save(ac);
					articleTypeService.update(new ArticleType(Integer.valueOf(ac.getType())));
					Map<String, Object> map = new HashMap<String,Object>();
					map.put("article_time", ac.getArticle_time());
					Article article = articleService.findOne(map);
					if(article != null) {
						ArticleFile articleFile = new ArticleFile();
						articleFile.setArticle_identify(uploadResult.getFileName());
						articleFile.setArticle_name(uploadResult.getMsg());
						articleFile.setArticle_url(uploadResult.getUploadUrl() + uploadResult.getFileName());
						articleFile.setA_id(String.valueOf(article.getId()));
						articleFileService.save(articleFile);
					}
					Pager pages = articleService.countPager3(null,1, 12);
					model.addAttribute("pages", pages);
					view = "/system/index";
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else {
			view = "/system/login";
		}
		return view;
	}
	
	
	@RequestMapping("/test")
	public ModelAndView mv(){
		return new ModelAndView("upload");
	}
}
